package com.xxxx.yoyo.adminweb;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xxxx.yoyo.bean.User;
import com.xxxx.yoyo.service.IUserService;
import com.xxxx.yoyo.service.impl.UserServiceImpl;


@WebServlet("/admin/userAdd")
public class UserAddServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private IUserService userService = new UserServiceImpl();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置导航条上的「客户管理]处于选中状态
		request.setAttribute("flag", 2);
		
		//请求转发，跳转到/admin/user_add.jsp
		request.getRequestDispatcher("/admin/user_add.jsp").forward(request,response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置编码格式为UTF-8,解决中文参数乱码
		request.setCharacterEncoding ("UTF-8");
		//设置导航条上的[客户管理]处于选中状态
		request.setAttribute("flag",2);
		
		//获取前端传递过来的参数username,password,name,phone,address
		String username = request.getParameter ("username");
		if(username.length() > 30){//用户名超过30个字
			//提示错误
			request.setAttribute("msg","添加失败！用户名不能超过30个字符！");
			
			//请求转发，跳转到/admin/user_add.jsp
			request.getRequestDispatcher("/admin/user_add.jsp").forward(request, response);
			return;
		}else {//用户名长度合适，判断该用户名是否已经存在
			User findedUser = userService.getUserByUsername(username);
			if(findedUser != null){//如果用户已经存在
				//提示错误
				request.setAttribute("msg","添加失败！该用户名已存在，请重新输入！");
				
				//请求转发，跳转到/admin/user_add.jsp
				request.getRequestDispatcher("/admin/user_add.jsp").forward(request, response);
				return;
			}
		}
		
		String password = request.getParameter("password");//明文密码
		String name = request.getParameter("name");
		if(name.length() > 20){//姓名超过了20个字
			//提示错误
			request.setAttribute("msg","添加失败！真实姓名不能超过20个字符！");
			
			//请求转发，跳转到/admin/user_add.jsp
			request.getRequestDispatcher("/admin/user_add.jsp").forward(request, response);
			return;
		}
		
		//获取手机号
		String phone = request.getParameter ("phone");
		try {
			long phoneLong = Long.parseLong(phone);
		}catch(Exception e){//出错说明，phone不全是数字
			//提示错误
			request.setAttribute("msg","添加失败！手机号有误！");
			
			//请求转发，跳转到/admin/user_add.jsp
			request.getRequestDispatcher("/admin/user_add.jsp").forward(request, response);
			return;
		}
		
		//获取地址
		String address = request.getParameter("address");
		if(address.length() > 100){//收货地址大于了100个字
			//提示错误
			request.setAttribute("msg","添加失败！详细地址不能大于100个字！");
			
			//请求转发，跳转到/admin/user_add.jsp
			request.getRequestDispatcher("/admin/user_add.jsp").forward(request, response);
			return;
		}
		
		//能到这里说明用户输入的信息都符合要求，开始添加
		//创建用户对象
		User user = new User(null, username, password, name, phone, address, 1);
		//把用户添加到数据库中
		boolean isSuccess = userService.addUser(user);
		if(isSuccess){//添加成功
			//重定向到用户列表页面
			response.sendRedirect ("/YoyoShop/admin/userList?page=1&size=16");
		}else {
			//提示错误
			request.setAttribute("msg","添加失败！");
			
			//请求转发，跳转到/admin/user_add.jsp
			request.getRequestDispatcher("/admin/user_add.jsp").forward(request, response);
			return;
		}
	}

}
